package com.liao.ticket.service.impl;

import com.alibaba.fastjson.JSON;
import com.liao.ticket.common.exception.ErrorCode;
import com.liao.ticket.dao.dos.ViewManager;
import com.liao.ticket.dao.pojo.User;
import com.liao.ticket.service.LoginService;
import com.liao.ticket.service.UserService;
import com.liao.ticket.util.*;
import com.liao.ticket.vo.Result;
import com.liao.ticket.vo.UserVo;
import com.liao.ticket.vo.params.LoginParams;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public Result login(LoginParams loginParams) {
        Integer type = loginParams.getType();
        if (type == null || type > 40 || type < 0) {
            return Result.fail(ErrorCode.USER_IDENTITY_UNCERTAIN);
        }
        String phone = loginParams.getPhone();
        String password = loginParams.getPassword();
        if (StringUtils.isBlank(phone) || StringUtils.isBlank(password)) {
            return Result.fail(ErrorCode.USER_PARAMS_FAIL);
        }
        User user = userService.findUserByAccountAndPass(phone, PassUtils.thicken(password));
        if (user == null) {
            return Result.fail(ErrorCode.USER_NO_EXIT);
        }
        // 判断用户的类型，如果跟 type 不同，则登录失败
        // 0 超级管理员 ; type 10-19 客户 ; 20-29 机场管理员 ; 30-39 航空公司管理员 ;
        if(user.getType()/10*10 != type){
            return Result.fail(ErrorCode.USER_NO_EXIT);
        }
        String token = JwtUtils.createToken(user.getId());
        redisTemplate.opsForValue().set("TOKEN_" + token, JSON.toJSONString(user), 1, TimeUnit.DAYS);
        return Result.success(token);
    }

    @Test
    void test(){
        String thicken = PassUtils.thicken("123456");
        System.out.println(thicken);
    }

    @Override
    public Result logout(String token) {
        if (token == null) {
            return Result.fail(ErrorCode.USER_TOKEN_FAIL);
        }
        redisTemplate.delete("TOKEN_" + token);
        return Result.success(null);
    }

    @Override
    public Result register(LoginParams loginParams) {
        String phone = loginParams.getPhone();
        String password = loginParams.getPassword();
        System.out.println(loginParams);
        if (StringUtils.isBlank(phone) || StringUtils.isBlank(password) || phone.length() > 11) {
            return Result.fail(ErrorCode.USER_PARAMS_FAIL);
        }
        User user = userService.findUserByPhone(phone);
        if (user != null) {
            return Result.fail(ErrorCode.USER_REGISTER_EXIT);
        }
        user = new User();
        user.setPhone(phone);
        user.setPassword(PassUtils.thicken(password));
        user.setType(10);
        userService.save(user);
        String token = JwtUtils.createToken(user.getId());
        redisTemplate.opsForValue().set("TOKEN_" + token, JSON.toJSONString(user), 1, TimeUnit.DAYS);
        return Result.success(token);
    }

    @Override
    public User checkToken(String token) {
        if (token == null) {
            return null;
        }
        Map<String, Object> map = JwtUtils.checkToken(token);
        if (map == null) {
            return null;
        }
        String json = redisTemplate.opsForValue().get("TOKEN_" + token);
        if (json == null) {
            return null;
        }
        User user = JSON.parseObject(json, User.class);
        return user;
    }
}
